package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.entity.Category;
import com.chen.entity.Dish;
import com.chen.entity.Setmeal;
import com.chen.exception.BusinessException;
import com.chen.mapper.CategoryMapper;
import com.chen.service.CategoryService;
import com.chen.service.DishService;
import com.chen.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //查看这个分类下有无菜品
        lqw.eq(Dish::getCategoryId,id);
        int count = dishService.count(lqw);
        if (count > 0){
            throw new BusinessException("当前分类下关联了菜品,不能删除");
        }

        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        //查看这个分类下有无套餐
        lqw1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lqw1);
        if (count1 > 0){
            throw new BusinessException("当前分类下关联了套餐,不能删除");
        }

        super.removeById(id);
    }
}